/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Adriano Classe DataSource
 */
public class DataSource {

    private String hostname;
    private String username;
    private String password;
    private String database;
    //private String port;
    private String log;
    private int port;
    private Connection connection;

    public DataSource() {
        try {

            //Local Connection
            hostname = "localhost";
            username = "root";
            password = "favela";
            port = 3306;
            database = "adrianotcc";

            //Server Connection
//            log = System.getenv("OPENSHIFT_MYSQL_DB_LOG_DIR");
//            hostname = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
//            port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
//            username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
//            password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
//            String URL = String.format("jdbc:mysql://%s:%s/tccadriano", hostname, port);
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());

            connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database, username, password);
            //connection = DriverManager.getConnection(URL, username, password); //Server Web

        } catch (SQLException e) {

            System.err.println("DATASOURCE  - Erro de conexão com  Banco - " + e.getMessage());
            System.err.println(hostname);
            System.err.println(port);
            System.err.println(username);
            System.err.println(password);
//            System.err.println(log);

        }
    }

    public Connection getConnection() {
        return connection;
    }

    public void closeConnection() {
        try {
            connection.close();
        } catch (SQLException e) {
            System.err.println("DATASOURCE - Erro ao fechar conexão" + e.getMessage());
        }
    }
}
